/**
 * 表格选中单元格搜索
 * @param {string}      field   字段名
 * @param {string|int}  value   值
 * @param {string|int}  mode    模式
 */
function tableCellSearch(field, value, mode = 1){
    switch(mode){
        case 1: //多选择的输入
            var filedDom = $('.dropdown-item[data-field="' + field + '"]');
            var searchBarDom = filedDom.parents('.search-bar');
            searchBarDom.find('button').html(filedDom.html());
            searchBarDom.find('input[type="hidden"]').val(field);
            searchBarDom.parent().find('input[type="text"]').val(value);
            break;
        case 2: //无选择的输入
            $('#'+field).val(value);
            break;
        case 3: //下拉框的选择
            var seledtDom = $('select[name="' + field + '"]');
            var lyearSelectDom = seledtDom.parent().find('.lyear-select');
            seledtDom.find('option[value="' + value + '"]').prop('selected', true);
            lyearSelectDom.find('input').val(seledtDom.find('option[value="' + value + '"]').html());
            lyearSelectDom.find('li').removeClass();
            lyearSelectDom.find('li[data-value="' + value + '"]').addClass('selected');
            break;
    }
    refreshTable();
}


/**
 * 传入时间戳转换时间格式
 * @param {int|string} timeStamp    时间戳
 * @returns {string}
 */
function makeData(timeStamp = 0){
    if(timeStamp > 0){
        var date = new Date(timeStamp * 1000);
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();
        var hour = date.getHours();
        var minute = date.getMinutes();
        var second = date.getSeconds();
        return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second;
    }
    return '-';
}


function showNotifyCompilation(msg, code, url = '', highest = false){
    switch (code){
        case 200:
            showNotify(msg, 'success', 1500, '', '', '', url, highest);
            break;
        case 204:
            showNotify(msg, 'info', 1500, '', '', '', url, highest);
            break;
        case 503:
        case 1001:
            showNotify(msg, 'warning', 1500, '', '', '', url, highest);
            break;
        default:
            showNotify(msg, 'danger', 1500, '', '', '', url, highest);
            break;
    }
}

const httpRequestMethod = ['get', 'post', 'deletes'];
/**
 * 二次确认
 * @param ajax
 * @param confirm
 * @param title
 */
function reconfirm(ajax, confirm, title){
    //二次确认
    layer.confirm(confirm, {
        icon: "doubt",
        title: title,
        closeBtn: 2,
        shadeClose: true,
    }, function (index) {
        layer.close(index);
        let loader = $('body').lyearloading({
            opacity: 0.2,
            spinnerSize: 'lg'
        });
        eval(ajax.method ? (httpRequestMethod.includes(ajax.method) ? ajax.method : 'post') : 'post')(ajax.url, ajax.param).success(res => {
            ajax.success && ajax.success(res);
            loader.destroy();
        }).error();
    });
}

/**
 * 打开新页面
 * @param data
 * @param title
 */
function openPage(data, title){
    let url = data.url;
    if(data.param && Object.keys(data.param).length > 0){
        url += '?';
        for(let key in data.param){
            url += key + '=' + data.param[key] + '&';
        }
        url = url.substring(0, url.length - 1);
    }
    layer.open({
        title: title,
        type: 2,
        content: url,
        maxmin: true,
        area: ['80%', '80%'],
        maxHeight: '80%',
        shadeClose: true,
    });
}


/**
 * Table表格的操作列，自定义按钮
 * @returns {string}
 */
function btnGroup (objHtml) {
    let html = '';
    for(let key in objHtml){
        let attr = '';
        //如果有attr属性
        if(objHtml[key]['attr']){
            for(let attrKey in objHtml[key]['attr']){
                attr += attrKey + '="' + objHtml[key]['attr'][attrKey] + '" ';
            }
        }
        html += '<span ' + attr + '><a href="#!" class="btn btn-sm btn-default ' + ((+key + 1) !== objHtml.length ? 'me-1' : '' ) + ' ' + objHtml[key]['a-class'] + '" title="' + objHtml[key]['title'] + '" data-bs-toggle="tooltip">';
        html += '<i class="mdi ' + objHtml[key]['icon'] + '"></i>';
        html += '</a></span>';
    }
    return html;
}

/**
 * 给按钮绑定事件
 * @returns {{}}
 */
function btnEvents(objHtml){
    let obj = {};
    for(let key in objHtml){
        //给obj里面增加一个对象
        obj['click .' + objHtml[key]['a-class']] = function (event, value, row, index) {
            event.stopPropagation();
            objHtml[key]['event'](event, value, row, index);
        };
    }
    return obj;
}

/**
 * 表格刷新
 */
function refreshTable(idName = 'table'){
    $('#'+idName).bootstrapTable('refresh');
}

// const RequestSingleton = (function() {
//     let instance;
//
//     function createInstance(url, method) {
//         return new HttpRequest(url, method);
//     }
//
//     return {
//         getInstance: function(url, method) {
//             if (!instance) {
//                 instance = createInstance(url, method);
//             }
//             return instance;
//         }
//     };
// })();

function post(url, data = {}){
    return new HttpRequest(url, 'POST').send(data);
    // return RequestSingleton.getInstance(url, 'POST').send(data)
}

function get(url, data = {}){
    return new HttpRequest(url, 'GET').send(data);
    // return RequestSingleton.getInstance(url, 'GET').send(data);
}

function deletes(url, data = {}){
    return new HttpRequest(url, 'DELETE').send(data);
}

function showSuccessNotify(msg, url = '', highest = false){
    showNotifyCompilation(msg, 200, url, highest);
}
function showErrorNotify(msg, url = '', highest = false){
    showNotifyCompilation(msg, 500, url, highest);
}

function showWarningNotify(msg, url = '', highest = false){
    showNotifyCompilation(msg, 503, url, highest);
}
function showInfoNotify(msg, url = '', highest = false){
    showNotifyCompilation(msg, 204, url, highest);
}
